<?php

namespace app\admin\traits;

use think\Collection;
use think\Model;

/**
 *
 * Trait MemberFromTrait
 * @package app\admin\traits
 */
trait MemberFromTrait
{
    // 注册来源
    private array $channelArr = [
        '0' => '其它',
        '1' => 'QQ',
        '2' => '支付宝',
        '3' => '微信pc扫码',
        '4' => '微信小程序',
        '5' => '微信公众号关注',
        '6' => '苹果App',
        '7' => '安卓App',
        '8' => '百度小程序',
        '9' => '抖音小程序',
        '10' => 'H5',
    ];

    public function wrapMemberChannelStr(Model $data)
    {
        $data->channelStr = $this->getMemberChannelStr($data);
    }

    public function wrapMemberCollectionChannelStr(Collection $collection)
    {
        $collection->each(function ($data) {
            $this->wrapMemberChannelStr($data);
        });
    }

    private function getMemberChannelStr(Model $data)
    {
        $channel = $data['channel'];

        if (is_null($channel)) {
            return '--';
        }

        // H5
        if ($channel == 10) {

            $fromChain = $data->getRelation('fromChain');
            if ($fromChain) {
                return $fromChain['showName'];
            }

            return '--';
        }

        return $this->channelArr[$channel] ?? '--';
    }

}
